package com.atguigu.campusintelligentassistantsystem.service;

import com.atguigu.campusintelligentassistantsystem.entity.ScholarshipApplication;
import com.atguigu.campusintelligentassistantsystem.entity.ScholarshipInfo;
import com.atguigu.campusintelligentassistantsystem.repository.ScholarshipApplicationRepository;
import com.atguigu.campusintelligentassistantsystem.repository.ScholarshipInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Service
public class ScholarshipService {

    @Autowired
    private ScholarshipInfoRepository scholarshipInfoRepository;

    @Autowired
    private ScholarshipApplicationRepository scholarshipApplicationRepository;

    /**
     * 发布奖学金信息
     * @param scholarshipInfo 奖学金信息
     * @return 保存后的奖学金信息
     */
    public ScholarshipInfo publishScholarshipInfo(ScholarshipInfo scholarshipInfo) {
        scholarshipInfo.setCreatedAt(LocalDateTime.now());
        return scholarshipInfoRepository.save(scholarshipInfo);
    }

    /**
     * 获取所有可申报的奖学金信息
     * @return 奖学金信息列表
     */
    public List<ScholarshipInfo> getAvailableScholarships() {
        return scholarshipInfoRepository.findByDeadlineAfterOrderByCreatedAtDesc(LocalDateTime.now());
    }

    /**
     * 根据ID获取奖学金信息
     * @param id 奖学金ID
     * @return 奖学金信息
     */
    public ScholarshipInfo getScholarshipInfoById(Long id) {
        Optional<ScholarshipInfo> scholarshipInfo = scholarshipInfoRepository.findById(id);
        return scholarshipInfo.orElse(null);
    }

    /**
     * 更新奖学金信息
     * @param scholarshipInfo 奖学金信息
     * @return 更新后的奖学金信息
     */
    public ScholarshipInfo updateScholarshipInfo(ScholarshipInfo scholarshipInfo) {
        return scholarshipInfoRepository.save(scholarshipInfo);
    }

    /**
     * 删除奖学金信息
     * @param id 奖学金ID
     */
    public void deleteScholarshipInfo(Long id) {
        scholarshipInfoRepository.deleteById(id);
    }

    /**
     * 提交奖学金申请
     * @param application 申请信息
     * @return 保存后的申请信息
     */
    public ScholarshipApplication submitApplication(ScholarshipApplication application) {
        LocalDateTime now = LocalDateTime.now();
        application.setCreatedAt(now);
        application.setUpdatedAt(now);
        application.setStatus("submitted"); // 默认状态
        return scholarshipApplicationRepository.save(application);
    }

    /**
     * 根据学生ID获取申请记录
     * @param studentId 学生ID
     * @return 申请记录列表
     */
    public List<ScholarshipApplication> getApplicationsByStudentId(Long studentId) {
        return scholarshipApplicationRepository.findByStudentIdOrderByCreatedAtDesc(studentId);
    }

    /**
     * 根据状态获取申请记录
     * @param status 状态
     * @return 申请记录列表
     */
    public List<ScholarshipApplication> getApplicationsByStatus(String status) {
        return scholarshipApplicationRepository.findByStatusOrderByCreatedAtDesc(status);
    }

    /**
     * 更新申请状态和审核意见
     * @param id 申请ID
     * @param status 状态
     * @param comments 审核意见
     * @return 更新后的申请信息
     */
    public ScholarshipApplication updateApplicationStatus(Long id, String status, String comments) {
        Optional<ScholarshipApplication> applicationOpt = scholarshipApplicationRepository.findById(id);
        if (applicationOpt.isPresent()) {
            ScholarshipApplication application = applicationOpt.get();
            application.setStatus(status);
            application.setReviewComment(comments);
            application.setUpdatedAt(LocalDateTime.now());
            return scholarshipApplicationRepository.save(application);
        }
        return null;
    }

    /**
     * 根据ID获取申请信息
     * @param id 申请ID
     * @return 申请信息
     */
    public ScholarshipApplication getApplicationById(Long id) {
        Optional<ScholarshipApplication> application = scholarshipApplicationRepository.findById(id);
        return application.orElse(null);
    }
}